package com.onpositive.wiki3.db.impl;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashSet;

import com.onpositive.wiki2.whole.model.ITemplateAccess;
import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.impl.CountingVocabalary.Pair;

public class TemplateColorer {

	public static void main(String[] args) {
		final WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		try {
			final Categories c = new Categories(wikiDBImpl, new File(
					"C:/work/content/relation.dat"), true);
			int l = 0;
			System.out.println(c.size());
			ITemplateAccess temlates = wikiDBImpl.getTemlates();
			PrintStream ss = new PrintStream(wikiDBImpl.getPath() + "/log.txt");
			CategoryTemplateStore st=new CategoryTemplateStore();
			for (Category m : c.all()) {
				int childCats = 0;
				if (!m.hasChildren()) {
					continue;
				}				
				CountingVocabalary vs = new CountingVocabalary();
				Category[] childCategories = m.getChildCategories();
				for (Category q : childCategories) {
					if (!q.hasChildren()) {
						
						String[] templates = temlates.getTemplates(q.id());
						boolean isObject = false;
						HashSet<String>sm=new HashSet<String>();
						for (String s : templates) {
							if (s.toLowerCase().contains("infobox")) {
								isObject = true;
							}
							String[] split = s.toLowerCase(). replace(' ', '_').split("_");
							for (String sq:split){
								if (sq.equals("infobox")){
									continue;
								}
								sm.add(sq);
							}
						}
						for (String ma:sm){
							vs.add(ma);
						}
						if (isObject) {
							// gs.childObject++;
						}
					} else {
						childCats++;
					}
				}

				ArrayList<Pair> all = vs.all();
				if (all.size() > 0) {
					Pair q = all.iterator().next();
					//System.out.println(q);
					if (q.i > ((childCategories.length - childCats) * 2) / 3) {
						st.record(m, childCategories.length - childCats, childCats, q.s,(q.i + 0.0)
								/ (childCategories.length - childCats));
					}
				}				
			}
			st.store(wikiDBImpl.getPath()+"/cattemplates.obj");
			System.out.println(l);
			System.out.println(c.size());
			// c.write(new File("C:/work/content/relation-st.dat"));

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
